package org.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.ssm.pojo.User;
import org.ssm.service.IUserService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

@RequestMapping("/list")
    public String list(HttpServletRequest httpServletRequest){
        List<User> userList =  userService.selelctAll();
        httpServletRequest.setAttribute("userList",userList);
        return  "list";
    }
@RequestMapping("/insert")
    public String insertUser(){
        return "insert";
}
    @RequestMapping("/insertUser")
    public String insert(int uid,String name,Integer age,String sex,String address,double sal){
        System.out.println(uid+name+age+sex+address+sal);
        userService.insertUser(uid,name,age,sex,address,sal);
        return "redirect:/user/pageList";
    }

    @RequestMapping("/delet/{uid}")
    public String delete( @PathVariable("uid") Integer uid){
        System.out.println(uid);
          userService.deleteUser(uid);
         return "delete";
    }
    @RequestMapping("/update/{uid}")
    public String updateUser( @PathVariable("uid") Integer uid,HttpServletRequest req){
        System.out.println(uid);
        req.setAttribute("uid",uid);
        return "update";
    }
    @RequestMapping("/updateUser/{uid}")
    public String update(@PathVariable("uid") Integer uid,String name,Integer age,String sex,String address,double sal){
        userService.updateUSer(uid,name,age,sex,address,sal);
        System.out.println(uid+name+age+sex+address+sal);
        return "redirect:/user/pageList";
    }
    @RequestMapping("/pageList")
    public String  pageList(String pageNum, String pageSize, HttpServletRequest req){
        if(StringUtils.isEmpty(pageNum)){
            pageNum = "1";//当前页数
        }
        if(StringUtils.isEmpty(pageSize)){
            pageSize = "5"; //每页的记录条数
        }
        PageHelper.startPage(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        List<User> userList = userService.selelctAll();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        req.setAttribute("userList" ,userList);
        req.setAttribute("pageInfo",pageInfo);
        return  "list";
    }

    @RequestMapping("/searchUser")
    public String searchUser(HttpServletRequest req,Integer uid,String name,String address){
      List<User> userList = userService.selectByCondition(uid,name, address);
      if (userList.size()==0){
          return "searcherror";
      }else {
          req.setAttribute("userSearchList",userList);
          return "search";
      }
    }
}
